package org.example.petadoption.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.example.petadoption.entity.Orders;
import org.example.petadoption.entity.OrdersUserProductDTO;
import org.example.petadoption.entity.Product;
import org.example.petadoption.entity.ResultBody;
import org.example.petadoption.service.IOrdersService;
import org.example.petadoption.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.math.BigDecimal;
import java.util.List;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author PetAdoption
 * @since 2025-10-20
 */
@RestController
@RequestMapping("/orders")
public class OrdersController {

    @Autowired
    IOrdersService ordersService;
    @Autowired
    IProductService productService;

    //查询所有订单
    @GetMapping("allOrders")
    public ResultBody allOrders() {
        List<Orders> ordersList = ordersService.list();
        return ResultBody.success("所有订单", ordersList);
    }

    //查询用户订单
    @PostMapping("/selectOrdersUser")
    public ResultBody selectOrders1(int id) {
        if (id <= 0) {
            return ResultBody.fail("用户ID不合法");
        }
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId, id);
        List<Orders> ordersList = ordersService.list(queryWrapper);
        return ResultBody.success("ID " + id + " 的用户订单" , ordersList);
    }

    //查询商家订单
    @PostMapping("/selectOrdersMerchant")
    public ResultBody selectOrders2(int id) {
        if (id <= 0) {
            return ResultBody.fail("商家ID不合法");
        }
        List<OrdersUserProductDTO> ordersList = ordersService.getOrdersByMerchant(id);
        return ResultBody.success("ID " + id + " 商家的订单" , ordersList);
    }


    //创建用户订单
    @PostMapping("/addOrdersUser")
    public ResultBody addOrdersUser(@RequestBody Orders orders) {

        // 1. 校验必要参数
        if (orders.getUserId() == null || orders.getProductId() == null || orders.getQuantity() == null) {
            return ResultBody.fail("缺少必要参数");
        }

        // 2. 查询商品信息
        Product product = productService.getById(orders.getProductId());
        if (product == null) {
            return ResultBody.fail("商品不存在");
        }

        // 3. 库存检查
        if (orders.getQuantity() > product.getStock()) {
            return ResultBody.fail("商品库存不足");
        }

        // 4. 自动填充订单信息
        orders.setProductName(product.getName());
        orders.setMerchantId(product.getMerchantId());
        orders.setProductPrice(product.getPrice());
        orders.setTotalAmount(product.getPrice().multiply(BigDecimal.valueOf(orders.getQuantity())));
        orders.setActualAmount(orders.getTotalAmount()); // 如果有优惠，可在这里计算
        orders.setStatus((byte) 0); // 0-待付款
        orders.setFreight(BigDecimal.ZERO);
        orders.setDiscount(BigDecimal.ZERO);
        orders.setOrderNo("ORD" + System.currentTimeMillis());

        // 5. 保存订单
        boolean saved = ordersService.save(orders);
        if (!saved) {
            return ResultBody.fail("订单创建失败");
        }

        // 6. 扣减商品库存
        product.setStock(product.getStock() - orders.getQuantity());
        productService.updateById(product);

        return ResultBody.success("订单创建成功",orders);
    }



}
